-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: support eval with ts syntax detection #56285
base: main
Are you sure you want to change the base?
module: support eval with ts syntax detection #56285
Conversation
Review requested:
|
7229405
to
f0f0e3c
Compare
f0f0e3c
to
728a1fd
Compare
3485014
to
1dd1cb5
Compare
2f87a9c
to
aeb77e9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I like this approach, it feels like a good way to unflag strip-types and provide convenience to the vast majority of users, while providing an option to be explicit for edge cases.
@joyeecheung should review the additions to lib/internal/process/execution.js
, I feel like I’ve worked in that file in the past and she’s had notes for me.
Node.js will first try to run the code as JavaScript, | ||
then it will try to run the code as TypeScript. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably say something about syntax detection here, that first it attempts to run as CommonJS JavaScript, and depending on the parse error thrown, will retry as ESM JavaScript or CommonJS TypeScript; and then potentially a third time as ESM TypeScript? I’m not even sure what the flow would be, which probably means that we should spell it out 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not an expert on how the js syntax detection is implemented but the flow is:
- run normally as javascript (this will perform the detection by trying cjs and esm)
- if invalid syntax, strip the types and try again
- if invalid again throw the first invalid syntax error enriched with the typescript message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The details of syntax detection are here: https://github.com/nodejs/node/blob/6012a4e9a2733b25a7023baa0a58bef85c9ebc5f/doc/api/esm.md#:~:text=at%20pjsonURL.-,DETECT_MODULE_SYNTAX,-(source)
The important part is to clarify which specific errors will trigger strip-types behavior. I assume there’s no overlap between the “retry as ESM” errors and the “retry with type stripping” errors, so the flow is no more expensive than the current detection flow: try first as CommonJS JavaScript, and retry as stripped types.
It gets even more complicated because doesn’t the strip-types flow itself use syntax detection? Like after it strips the types, then it initially tries to run as CommonJS, then retries as ESM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it repeats the detection twice but its trasparent from my implementation. I'm not sure it can be skipped
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #56285 +/- ##
==========================================
- Coverage 88.55% 88.54% -0.01%
==========================================
Files 657 657
Lines 190243 190425 +182
Branches 36536 36553 +17
==========================================
+ Hits 168461 168616 +155
- Misses 14963 14982 +19
- Partials 6819 6827 +8
|
7dd20f3
to
3fd9958
Compare
3fd9958
to
cdd305c
Compare
Previous attempt: #56273
What is the problem?
Before this PR when
--experimental-strip-types
was enabled,--eval
would always parse the input as typescript.If unflagged, the typescript parser would throw different errors on invalid syntax so unflagging would become a breaking change.
With this PR when running
--eval
and--experimental-strip-types
is enabled, if parsing the code fails we try again with typescript parser.If it fails again we throw the original error, adding the typescript parser message.
In this way the error is the original error and it's not a breaking change.
Example:
This PR also add two new
--input-type
:module-typescript
commonjs-typescript
So that if the syntax is known we can reduce the overhead of multiple parsing.
If the
-typescript
input is passed we can throwERR_INVALID_TYPESCRIPT_SYNTAX
safely